package org.surelink.radius.console;

import de.tototec.cmdoption.CmdOption;
import de.tototec.cmdoption.CmdlineParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.surelink.radius.RadiusConfig;
import org.surelink.radius.console.message.Request;
import org.surelink.radius.console.message.RespBase;
import org.surelink.radius.console.message.Response;

@Component
public class LoginProcess extends BaseProcess{

    @Autowired
    private RadiusConfig radcfg;

    class Config{
        @CmdOption(names = {"--help"}, description = "打印帮助",isHelp = true)
        private boolean help;

        @CmdOption(names = {"login"} ,hidden = true)
        private boolean login;


        @CmdOption(names = {"--user", "-u"},args={"user"}, minCount = 1,maxCount = 1,description = "管理用户")
        String user;

        @CmdOption(names = {"--passwd", "-p"},args={"passwd"}, minCount = 1,maxCount = 1,description = "管理密码")
        String passwd;
    }

    public LoginProcess() {
        setOperate("login");
    }

    @Override
    public Response process(Request req) throws Exception {
        Config config = new Config();
        CmdlineParser cp = new CmdlineParser(config);
        cp.setProgramName(getOperate());
        cp.parse(req.getCommand().split("\\s+"));

        if(config.help)
        {
            StringBuilder sb = new StringBuilder();
            cp.usage(sb);
            return new Response(RespBase.SUCCESS.getCode(),sb.toString());
        }

        if(config.user.equals(radcfg.getString("console.admin"))
                &&config.passwd.equals(radcfg.getString("console.passwd")))
        {
            return new Response("ok");
        }

        return  RespBase.LOGIN_ERR;



    }
}
